package sample.ui

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.netease.core.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.script.tes.colorInt
import kotlinx.coroutines.script.tes.lifecycleOwner

class PopAlert private constructor(
    context: Context,
    theme: Int
) : Dialog(context, theme) {
    private var params: Params? = null
    private var taskCountdown: Job? = null
    override fun show() {
        super.show()
        var countdown = params?.countdown ?: 0
        if (countdown > 0) {
            params?.tConfirm?.lifecycleOwner?.lifecycleScope?.launch(Dispatchers.Main) {
                while(countdown > 0) {
                    params?.tConfirm?.alpha = 0.5f
                    params?.tConfirm?.isEnabled = false
                    params?.tConfirm?.text = "${params?.confirmText}(${countdown}s)"
                    delay(1000L)
                    countdown --
                }
                params?.tConfirm?.text = "${params?.confirmText}"
                params?.tConfirm?.alpha = 1f
                params?.tConfirm?.isEnabled = true
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        taskCountdown?.cancel()
    }

    private class Params {
        lateinit var mContext: Context
        lateinit var tCancel: TextView
        lateinit var tConfirm: TextView
        lateinit var tTitle: TextView
        lateinit var tMessage: TextView
        var keepAfterMenuClicked = false
        var canCancel = true
        var mTitle: String? = null
        var mMessage: String? = null
        var cancelText: String? = null
        var confirmText: String? = null
        var cancelCallback: DialogInterface.OnClickListener? = null
        var confirmCallback: DialogInterface.OnClickListener? = null
        var countdown: Int = 0
        var textGravity: Int = Gravity.CENTER
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.mContext = context
        }

        fun setMessage(message: String, gravity: Int = Gravity.CENTER): Builder {
            p.mMessage = message
            p.textGravity = gravity
            return this
        }

        fun setConfirmCountdown(value: Int): Builder {
            p.countdown = value
            return this
        }

        fun setCancelable(cc: Boolean): Builder {
            p.canCancel = cc
            return this
        }
        fun setKeepAfterMenuClicked(keep: Boolean): Builder {
            p.keepAfterMenuClicked = keep
            return this
        }

        fun setPositiveButton(
            text: String,
            callback: DialogInterface.OnClickListener
        ): Builder {
            p.confirmText = text
            p.confirmCallback = callback
            return this
        }

        fun setTitle(title: String): Builder {
            p.mTitle = title
            return this
        }

        fun show() {
            create().show()
        }

        fun setNegativeButton(
            text: String,
            callback: DialogInterface.OnClickListener
        ): Builder {
            p.cancelText = text
            p.cancelCallback = callback
            return this
        }

        fun create(): PopAlert {
            val dialog = PopAlert(p.mContext, R.style.Theme_Light_NoTitle_NoShadow_Dialog)
            val dip1 = (p.mContext.resources.displayMetrics.density + 0.5f).toInt()
            val window = dialog.window
            if (window != null) {
                window.decorView.setPadding(dip1 * 40, 0, dip1 * 40, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = lp
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.mContext)
                .inflate(R.layout.pop_alert, null)
            p.tMessage = view.findViewById(R.id.message)
            p.tCancel = view.findViewById(R.id.cancel)
            p.tConfirm = view.findViewById(R.id.confirm)
            p.tTitle = view.findViewById(R.id.title)

            p.tMessage.text = p.mMessage
            p.tMessage.isVisible = !p.tMessage.text.isNullOrEmpty()
            p.tMessage.gravity = p.textGravity
            p.tTitle.text = p.mTitle // 新ui不需要提示文字
            p.tTitle.isVisible = !p.tTitle.text.isNullOrEmpty()

            view.setBackgroundResource(R.drawable.z_ffffff_10)
            p.tTitle.setTextColor("#333333".colorInt)
            p.tMessage.setTextColor("#333333".colorInt)
            p.tCancel.setTextColor("#333333".colorInt)
            p.tConfirm.setTextColor("#333333".colorInt)
            p.tCancel.setBackgroundResource(R.drawable.s_ffffff_10_left_bottom)
            p.tConfirm.setBackgroundResource(R.drawable.s_ffffff_10_right_bottom)

            if (p.cancelCallback != null) {
                p.tCancel.text = p.cancelText
                p.tCancel.setOnClickListener {
                    dialog.dismiss()
                    p.cancelCallback?.onClick(dialog, 0)
                }
                p.tCancel.visibility = View.VISIBLE
            } else {
                p.tConfirm.setBackgroundResource(R.drawable.s_ffffff_10_bottom)
                p.tCancel.visibility = View.GONE
            }

            if (p.confirmCallback != null) {
                p.tConfirm.text = p.confirmText
                p.tConfirm.setOnClickListener {
                    if (!p.keepAfterMenuClicked) {
                        dialog.dismiss()
                    }
                    p.confirmCallback?.onClick(dialog, 1)
                }
            } else {
                p.tConfirm.setOnClickListener {
                    dialog.dismiss()
                }
            }


            dialog.params = p
            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }
    }
}